﻿using ByHand.Model;
using ByHand.Services.Code;
using ByHand.Services.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FrontEnd.Controllers
{
    public class LogController : Controller
    {
        private readonly IOrderLogService _OrderLogService;
        public LogController(IOrderLogService OrderLogService)
        {
            _OrderLogService = OrderLogService;
        }

        public ActionResult LogOrder(int orderid, string actiontype, string username, string usertype)
        {
            var log = new OrderLog()
            {
                OrderID = orderid,
                ActionType = actiontype,
                UserName = username,
                UserType = usertype,
                DateTime = DateTime.Now
            };
            if (string.IsNullOrWhiteSpace(username))
            {
                log.UserName = Utility.GenerateId();
            }
            if (User.Identity.IsAuthenticated)
            {
                log.ByHandName = User.Identity.Name;
                log.ActionType = log.ActionType.Replace(log.UserName, log.ByHandName);
            }
            _OrderLogService.Add(log);
            return Json(new { result = true, uniqueid = log.UserName });
        }
    }
}
